<?php

namespace App\Http\Controllers\user;

use App\Dao\BillDao;
use App\Dao\ProjectDao;
use App\Dao\TaskDao;
use App\Dao\UsersDao;
use App\Exceptions\SysExceptions;
use App\Services\Cache;
use App\Services\Code;
use App\Services\Logger;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;

class TaskController extends BaseController
{

   public function gudongFen(){
            $users = UsersDao::get_user_builder()->where('vip_level_index',5);
            if($users->exists()){
                $users = $users->get()->toArray();
                foreach ($users as $value){
                    try {
                        DB::beginTransaction();
                        //获取团队的消费值
                        $xiaofeis = ProjectDao::get_project_user_builder([

                        ])->whereIn('user_id',explode(',',$value['spread_ids']))->sum('price');

                        if($xiaofeis < 1){
                            continue;
                        }

                        $UserMoneys = 0.01;


                        if($xiaofeis  > 50000 && $xiaofeis <= 100000){
                            $UserMoneys = 0.02;
                        }
                        BillDao::moneyNums($value['user_id'],(bcmul($xiaofeis,$UserMoneys,2)),'平台股东的奖励!',9998);
                        DB::commit();

                    }catch (Exception $exception){
                        DB::rollBack();
                        Logger::gu_gond_event("=======>a",[$exception->getMessage()]);
                        continue;
                    }
                }
            }
   }



    public function user_spread_task(){
        $last_time = Cache::getCacheValue('user_spread_task_time');
        if(Cache::hasCache('user_spread_task_time')){
            $last_time = time();
        }
        $uesrs = UsersDao::get_user_builder()
            ->where('spread_id','<>',0)
            ->orderByDesc('user_id');
        DB::update("update df_users set spread_ids = ',' WHERE spread_ids IS NULL");
        if($uesrs->exists()){
            $uesrs = $uesrs->get()->toArray();
            foreach ($uesrs as $value){
                $spread_id = $value['spread_id'];
                while(true){
                    if($spread_id == 0){
                        break;
                    }
                    $userInfo =  UsersDao::get_user_builder(['user_id'=>$spread_id])->first()->toArray();
                    $spread_ids = explode(',',$userInfo['spread_ids']);

                    if(!in_array($value['user_id'],$spread_ids)){
                        UsersDao::get_user_builder(['user_id'=>$spread_id])->update([
                            'spread_ids'=>$userInfo['spread_ids'].$value['user_id'].','
                        ]);
                    }
                    $spread_id = $userInfo['spread_id'];
                }
            }
        }
        Cache::setCache('user_spread_task_time',time());
    }




}
